$(document).ready(function () {
    Date.prototype.Format = function (fmt) { //author: meizz   
        var o = {
            "M+": this.getMonth() + 1,                 //月份   
            "d+": this.getDate(),                    //日   
            "h+": this.getHours(),                   //小时   
            "m+": this.getMinutes(),                 //分   
            "s+": this.getSeconds(),                 //秒   
            "q+": Math.floor((this.getMonth() + 3) / 3), //季度   
            "S": this.getMilliseconds()             //毫秒   
        };
        if (/(y+)/.test(fmt))
            fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
        for (var k in o)
            if (new RegExp("(" + k + ")").test(fmt))
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        return fmt;
    };

    $("#refresh").click(function () {
        $("#main").hide();
        $("#loading").show();
        $.get("http://localhost:8923/now", function (data) {
            var obj = eval("(" + data + ")");
            var t = new Date(obj.time);
            var y = t.getFullYear();
            var m = t.getMonth();
            var d = t.getDate();
            var h = t.getHours();
            var mn = t.getMinutes();
            $("#time").text(new Date(y, m, d, h + 8, mn));
            $("#img").attr("src", obj.name.substring(1));
            $("#main").show();
            $("#loading").hide();
        });
    });

    $("#all").click(function () {
        $.get("http://localhost:8923/all", function (data) {
            alert(data);
        });
    });

    $("#start").click(function () {
        $.get("http://localhost:8923/dynamic", function(data){
            var i = 0; var len = data.length;
            var tickFn = function(){
                if (i >= len) return;
                $("#img").attr("src", "./imgs/" + data[i]);
                i++;
                setTimeout(tickFn, 300);
            };
            setTimeout(tickFn, 300);
        });
    });

    window.setTimeout(function () {
        $("#refresh").click();
    }, 1000 * 60 * 10);
});
